package com.chen.dao.impl;

import com.chen.dao.AddressDao;
import com.chen.entity.Address;

import com.chen.entity.Category;
import com.chen.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class AddressDaoImpl implements AddressDao {

    Connection conn=DBUtil.getConnection();
    QueryRunner qr=new QueryRunner();

    @Override
    public int save(Address address) {
        String sql="INSERT INTO  tb_address(address_id,consignee_name,user_phone, " +
                " province,town,county,address,user_id) VALUES (?,?,?,?,?,?,?,?)";
        Object[] paras={address.getAddress_id(),address.getConsignee_name(),address.getUser_phone(),
                        address.getProvince(),address.getTown(),address.getCounty(),
                        address.getAddress(),address.getUser_id()};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(Address address) {
        String sql="UPDATE  tb_address SET  consignee_name=?,user_phone=?,province=?,town=?,county=?,address=?,user_id=? WHERE address_id=?   ";
        Object[] paras={address.getConsignee_name(),address.getUser_phone(),
                address.getProvince(),address.getTown(),address.getCounty(),
                address.getAddress(),address.getUser_id(),address.getAddress_id()};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(Object id) {
        String sql="DELETE FROM tb_address WHERE address_id  = ?  ";
        Object[] paras={id};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Address findById(Object key) {
        String sql="SELECT * FROM tb_address where address_id  = ?";
        Object[] paras={key};
        try {
            Address address=qr.query(conn,sql,paras,new BeanHandler<Address>(Address.class));
            return address;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Address> findAll() {
        String sql="SELECT * FROM tb_address";
        Object[] paras={};
        try {
            List<Address> addressList=qr.query(conn,sql,paras,new BeanListHandler<Address>(Address.class));
            return addressList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Address> findSearch(String keyword) {
        return null;
    }

    @Override
    public List<Address> findPage(int page, int pagesize) {
        String sql="SELECT * FROM tb_address limit ?,?";
        Object[] paras={(page-1)*pagesize,pagesize};
        try {
            List<Address> addressList=qr.query(conn,sql,paras,new BeanListHandler<Address>(Address.class));
            return addressList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long count() {
        return 0;
    }

    @Override
    public List<Address> findByUserId(String userId) {
        String sql="SELECT * FROM tb_address where user_id = ?";
        Object[] paras={userId};
        try {
            List<Address> addressList=qr.query(conn,sql,paras,new BeanListHandler<Address>(Address.class));
            return addressList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
